package com.blue.bluecode.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blue.bluecode.R;
import com.blue.bluecode.entity.Person;
import com.blue.bluecode.utils.BitmapUtil;

import java.util.List;

/**
 * PersonViewHolder是内部类
 * Created by bluewaitor on 2015/4/15.
 */

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

    /**
     * 数据源
     */
    List<Person> persons;
    Context mContext;
    /**
     * Adapter的构造方法
     * @param persons
     */
    public RVAdapter(Context context, List<Person> persons) {
        this.persons = persons;
        this.mContext = context;
    }

    /**
     * PersonViewHolder用来找到layout中的组件存储起来
     */
    class PersonViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        TextView tvPersonName;
        TextView tvPersonAge;
        ImageView ivPersonPhoto;

        public PersonViewHolder(View itemView) {
            super(itemView);
            /**
             * 在下面onCreateViewHolder方法中会被调用,所以能够获取到ID
             */
            cardView = (CardView) itemView.findViewById(R.id.cv);
            tvPersonName = (TextView) itemView.findViewById(R.id.person_name);
            tvPersonAge = (TextView) itemView.findViewById(R.id.person_age);
            ivPersonPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
        }
    }


    /**
     * 创建View的时候
     * @param viewGroup 用来得到上下文
     * @param i 位置
     * @return 返回ViewHolder
     */
    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_card_view,viewGroup,false);
        //初始化pvh并返回
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    /**
     * 绑定ViewHolder,用来设置数据
     * @param personViewHolder
     * @param i
     */
    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
        personViewHolder.tvPersonName.setText(persons.get(i).getName());
        personViewHolder.tvPersonAge.setText(persons.get(i).getAge());
//        personViewHolder.ivPersonPhoto.setImageResource(persons.get(i).getPhotoId());
        Bitmap bitmap = BitmapUtil.decodeSuitableBitmapFromResource(mContext.getResources(), persons.get(i).getPhotoId(), 40, 40);
        personViewHolder.ivPersonPhoto.setImageBitmap(bitmap);
    }

    /**
     * 将ViewHolder附到RecyclerView
     * @param recyclerView
     */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    /**
     * 获得有多少个Item
     * @return
     */
    @Override
    public int getItemCount() {
        return persons.size();
    }
}
